/*
 * COAPI - Copyright (C) 2019-2020 Cocheer Tech. Co., Ltd.
 * All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "coapi_logger.h"
#include "coapi_tts.h"
#include "coapi_register.h"
#include "coapi_record.h"
#include "coapi_devsta.h"
#include "coapi_common.h"
#include "coapi_stream.h"
#include "coapi_devsta.h"

#include "coapi_app.h"
#include "coapi_speech.h"
#include "coapi_record.h"
#include "coapi_funchub.h"

// #include "aos/aos.h"

static int data_feed_cbk(char *buf, int size, int status)
{
    static unsigned char count = 0;
    int ret = coapi_stream_feed(buf, size, status == 0 ? RECORD_CONTINUES : RECORD_END);
    if (ret < 0) {

    } else if (ret == VAD_STOP) {
    }

    count++;
    if (count >= 8) {
        coapi_stream_cancel();
        //coapi_stream_feed(buf,size, RECORD_CANCEL);
        count = 0;
    }
    return ret;
}

static void stream_cancel_test(void)
{
    unsigned count = 0;
    while (1) {

        xdebug("===========count: %d ============", count);
        if (count % 2 == 0) {
            int ret = coapi_stream_init(voice_callback, FUNC_SPEECH, RESULT_SDS, AI_VAD, TYPE_PCM);
            if (ret < 0) {
                xerror("not cloud registered !");
                sleep(3);
                continue;
            }

            ret = coapi_record_create(data_feed_cbk);
            if (ret) {
                xerror("record create fail !");
                sleep(3);
                continue;
            }
        } else {
            coapi_speech_start();
            sleep(5);
        }

        count++;

        if (count >= 10)
            break;
    }
    return;
}

static void text_ai_test(void)
{
    coapi_text_dial(voice_callback, "返回", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "拍照", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "接受聊天", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "挂断聊天", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "打开微信", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "打开相机", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "打开相机", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "我想看西游记第五集", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "播放第6集", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "我想看刘德华的MV", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "我想看张学友吻别的MV", RESULT_NLU_SDS);
    sleep(5);
}

static void dev_sta_test(char *devid)
{
#if 0
	unsigned int dpid = 101000022; /* 需在云端平台创建 */

	static unsigned char flag = 1;

    if(flag) { 
        //上传通讯录，不需要每次都上传，只有通讯录更新时才需要上传
        unsigned char addrbook[256];
        memset(addrbook, 0, 256);
        sprintf(addrbook, "{\"deviceId\":\"%s\",\"names\":[\"小酷\",\"小乐\",\"琦琦\"]}", devid);
        coapi_devsta_string(NULL,NULL,dpid, QOS_ONE, addrbook, strlen(addrbook));
        flag = 0;
        sleep(10);
    }


    coapi_text_dial(voice_callback, "我要打电话给小酷", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "我要打电话给小乐", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "我要打电话给琦琦", RESULT_NLU_SDS);
#endif

#if 0
    sleep(5);
    printf("==============打开灯光 =============\n");
    coapi_text_dial(voice_callback, "打开灯光", RESULT_NLU_SDS);
    sleep(5);
    printf("==============灯光调暗 =============\n");
    coapi_text_dial(voice_callback, "灯光调暗", RESULT_NLU_SDS);
    sleep(5);
    printf("==============自然光模式 =============\n");
    coapi_text_dial(voice_callback, "自然光模式", RESULT_NLU_SDS);
#endif

    sleep(5);
    printf("==============音量增加 =============\n");
    coapi_text_dial(voice_callback, "音量增加", RESULT_NLU_SDS);
    sleep(5);
    printf("==============打开取暖 =============\n");
    coapi_text_dial(voice_callback, "打开取暖", RESULT_NLU_SDS);
    sleep(5);
    printf("==============关闭取暖 =============\n");
    coapi_text_dial(voice_callback, "关闭取暖", RESULT_NLU_SDS);
    sleep(5);
    printf("==============取暖3档 =============\n");
    coapi_text_dial(voice_callback, "取暖3档", RESULT_NLU_SDS);

    sleep(5);
    printf("==============设置680 =============\n");
    coapi_text_dial(voice_callback, "设置680", RESULT_NLU_SDS);
}

static void funbuh_test(void)
{
    /* char *data = "{\"type\":\"省市台\",\"province\":\"广东\",\"city\":\"深圳\",\"name\":\"\"}"; */
    char *data = NULL;
    printf("==============电台测试: %s =============\n", data);
    coapi_funchub_start(FUNC_ID_AREQ_RADIO, data);
    sleep(5);

    printf("==============获取闹铃列表: %s =============\n", data);
    coapi_funchub_start(FUNC_ID_ECI_ALARMLIST, data);
    sleep(5);

    return;
}

static void alarm_test(void)
{
    coapi_text_dial(voice_callback, "一分钟后提醒我喝水", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "查询闹钟", RESULT_NLU_SDS);
    sleep(5);
    coapi_text_dial(voice_callback, "删除闹钟", RESULT_NLU_SDS);
}

void coapi_func_test(char *devId)
{
    /* text_ai_test(); */

    /* stream_cancel_test(); */

    /* dev_sta_test(devId); */

    /* funbuh_test(); */

    /* alarm_test(); */
}